package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import managers.Nimbus;
import mappers.MessageMapper;


public class MainWindowGUI {
	
	private JFrame mainFrame;
	private JMenuBar menuBar;
	private JMenu menu, menu2, menu3, menu4, menu5, menu6;
	private JMenuItem exitMI, showRoomMI, addRoomMI,
							aboutMI, showMessagesMI, composeMessageMI, printMI, addStudyMI,
							addTeacherMI, showTeachersMI;
	private JTable roomsTable;
	private String[] columnNames = {"Room name", "Is in use?"};
	private JScrollPane tableScrollPane, newsScrollPane;
	private JPanel tablePanel, panelRight, panelRightSide;
	private JSeparator js;
	
	public MainWindowGUI() {
		new Nimbus();
		mainFrame = new JFrame();
		mainFrame.setTitle("Main");
		mainFrame.setLayout(new BorderLayout());
		mainFrame.setSize(700, 500);
		
		menuBar = new JMenuBar();
		
		menu = new JMenu("File");
		menu2 = new JMenu("Rooms");
		menu3 = new JMenu("Study");
		menu4 = new JMenu("Teachers");
		menu5 = new JMenu("Messages");
		menu6 = new JMenu("Help");
	
		menuBar.add(menu);
		menuBar.add(menu2);
		menuBar.add(menu3);
		menuBar.add(menu4);
		menuBar.add(menu5);
		menuBar.add(menu6);
		
		printMI = new JMenuItem("Print");
		exitMI = new JMenuItem("Exit");
		exitMI.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);			
			}	
		});
		
		menu.add(printMI);
		menu.add(exitMI);
		
		showRoomMI = new JMenuItem("Show rooms");
		addRoomMI = new JMenuItem("Add room");
		addRoomMI.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				AddRoomGUI addRoomGUI = new AddRoomGUI();			
			}	
		});
		
		menu2.add(showRoomMI);
		menu2.add(addRoomMI);
		
		addStudyMI = new JMenuItem("Add Study");
		addStudyMI.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				AddStudyGUI addStudyGUI = new AddStudyGUI();		
			}
		});
		
		menu3.add(addStudyMI);
		
		showTeachersMI = new JMenuItem("Show Teachers");
		showTeachersMI.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ShowTeachersGUI showTeachersGUI = new ShowTeachersGUI();	
			}
		});
		
		addTeacherMI = new JMenuItem("Add Teacher(s)");
		addTeacherMI.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				AddTeacherGUI addTeacherGUI = new AddTeacherGUI();
			}
		});
		menu4.add(showTeachersMI);
		menu4.add(addTeacherMI);
		
		
		showMessagesMI = new JMenuItem("Show Messages");
		composeMessageMI = new JMenuItem("Compose Message");
		composeMessageMI.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ComposeMsgGUI composeMsgGUI = new ComposeMsgGUI();		
			}
		});
		
		menu5.add(showMessagesMI);
		menu5.add(composeMessageMI);
		
		aboutMI = new JMenuItem("About");
		aboutMI.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				AboutGUI aGUI = new AboutGUI();			
			}	
		});
		
		menu6.add(aboutMI);
		
		Object[][] data = {{"sdsd", "yes"}, {"sdsad", "asdsad"}};
		
		roomsTable = new JTable(data, columnNames);
		
		tableScrollPane = new JScrollPane(roomsTable);
		roomsTable.setFillsViewportHeight(true);
		roomsTable.getColumnModel().getColumn(0).setPreferredWidth(100);
		roomsTable.getColumnModel().getColumn(1).setPreferredWidth(90);
		roomsTable.setBorder(BorderFactory.createLineBorder(Color.black));
		roomsTable.getTableHeader().setReorderingAllowed(false);
		
		tablePanel = new JPanel(new BorderLayout());
		tablePanel.setBounds(10, 10, 300, 400);
		tablePanel.setLayout(new BorderLayout());
		tablePanel.add(roomsTable.getTableHeader(), BorderLayout.NORTH);
		tablePanel.add(roomsTable, BorderLayout.WEST);
		tablePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		js = new JSeparator(JSeparator.VERTICAL);
		
		//----------------------------------------------
		
		panelRight = new JPanel();
		panelRight.setLayout(new BoxLayout(panelRight, BoxLayout.PAGE_AXIS));
		panelRight.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		JTextPane newsTextPane = new JTextPane();
		//newsTextPane.setText("Velkommen til det nye system");
		newsTextPane.setEditable(false);
		StyledDocument doc = newsTextPane.getStyledDocument();
		MessageMapper mMapper = new MessageMapper();
		
		SimpleAttributeSet subject = new SimpleAttributeSet();
		StyleConstants.setForeground(subject, Color.BLACK);
		StyleConstants.setBold(subject, true);
		StyleConstants.setFontSize(subject, 18);
		
		SimpleAttributeSet text = new SimpleAttributeSet();
		StyleConstants.setFontSize(text, 14);
		
		for(int i = 0; i < mMapper.showAllMessages().size(); i++) {
			
			try{
			
			doc.insertString(0, mMapper.showAllMessages().get(i).getMessageText() + "\n\n"
					,text);		
			
			doc.insertString(0, mMapper.showAllMessages().get(i).getSubject() + "\n\n", subject);

			
			doc.insertString(0, mMapper.showAllMessages().get(i).getTime() +
					"\n\n", null);
			doc.insertString(0, "-----------------------------------------" +
					"---------------------------------------------------\n", null);
			
			} catch(Exception e) {
				System.out.println(e);
			} 		
		}
		
		
		
		newsScrollPane = new JScrollPane(newsTextPane);
		newsScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		newsScrollPane.setPreferredSize(new Dimension(200, 145));
		newsScrollPane.setMinimumSize(new Dimension(10, 10));		
		panelRight.add(newsScrollPane);
		
		panelRightSide = new JPanel(new BorderLayout());
		panelRightSide.add(js, BorderLayout.WEST);
		panelRightSide.add(panelRight, BorderLayout.CENTER);

		mainFrame.add(tablePanel, BorderLayout.WEST);
		mainFrame.add(panelRightSide, BorderLayout.CENTER);
		mainFrame.setJMenuBar(menuBar);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setVisible(true);
	}
}
